package vcp.unittests;


import static org.junit.Assert.*;

import java.awt.List;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import client.Message;
import db.parking.beans.*;
import db.tables.*;


import run.ServerFrame;
import server.SimpleServer;
import server.SimpleServer.InvalidException;


public class OccasionalCheck extends TestCase {

	private ServerFrame server;
	private List liste = new List();
	private Orders o1;
	private Subscriber s1;
	private SimpleServer simple;
	private Message msg = null;


	public OccasionalCheck(String name) {
		super(name);
	}


	@Before
	public void setUp() throws Exception {
		server= new ServerFrame(12345);
		server.setDbUser("root");
		server.setDbPass("31387650");
		server.setDbHost("localhost");
		server.setDbName("vcp");
		server.setDbPort("3306");
		
		
		server.listen();
		msg = new Message();
		AdminCustomer.update("2", 0); //init amount to 0
	}

	@After
	public void tearDown() throws Exception {
	}


	
	@Test
	public void test1() throws Exception {
		
		//Test1 - Occasional Customer parks for 2 hours
		float expected = -10;
		
		long time = System.currentTimeMillis();// Set current time at this second to StartTime
		Timestamp currTimestamp = new Timestamp(time);
		Timestamp leaveTime = new Timestamp(time);
		leaveTime.setHours(currTimestamp.getHours() + 2);
		
		o1 = new Orders("2", "Occasional","2",
				currTimestamp, leaveTime, 1, "e@e.com",
				1);
		
		AdminCustomer.update(o1.getUid(), 0);
		
		msg.setObj(o1);
		server.getServer().AddOccasionalOrder(msg);
		server.getServer().CheckOut(msg);
		float result = (float) AdminCustomer.getCustomerBalance(o1.getUid()).getAmount();
		
		Assert.assertEquals(expected,result);	
		
		}
	
@SuppressWarnings("deprecation")
public void test2() throws Exception {
		
		//Test2 - Occasional Customer parks for 2 hours
		float expected = 10;
		
		long time = System.currentTimeMillis();// Set current time at this second to StartTime
		Timestamp currTimestamp = new Timestamp(time);
		Timestamp leaveTime = new Timestamp(time);
		leaveTime.setHours(currTimestamp.getHours() + 2);
		
		o1 = new Orders("2", "Occasional","2",
				currTimestamp, leaveTime, 1, "e@e.com",
				1);
		float result = server.getServer().calculatePayment(o1);		
		
		Assert.assertEquals(expected,result);	
		
		}
	
public void test3() throws Exception {
	
	//Test3 - Calculate function received an unknown type
	String expected = "Unknown Customer Type";
	
	long time = System.currentTimeMillis();// Set current time at this second to StartTime
	Timestamp currTimestamp = new Timestamp(time);
	Timestamp leaveTime = new Timestamp(time);
	leaveTime.setHours(currTimestamp.getHours() + 2);
	
	o1 = new Orders("2", "Unknown","2",
			currTimestamp, leaveTime, 1, "e@e.com",
			1);
	try{
	
		 server.getServer().calculatePayment(o1);	
	}
	catch(InvalidException e)
	{
		Assert.assertEquals(expected,e.getMessage());	
	}
	
}

public void test4() throws Exception {
	
	//Test4 - Check Occasional Customer that leave time is after start time
	String expected = "Start Time Can't Be After Leave Time";
	
	long time = System.currentTimeMillis();// Set current time at this second to StartTime
	Timestamp currTimestamp = new Timestamp(time);
	Timestamp leaveTime = new Timestamp(time);
	leaveTime.setHours(currTimestamp.getHours() -2);
	
	o1 = new Orders("2", "Occasional","2",
			currTimestamp, leaveTime, 1, "e@e.com",
			1);
	try{
	
		server.getServer().calculatePayment(o1);	
	}
	catch(InvalidException e)
	{
		Assert.assertEquals(expected,e.getMessage());	
	}
	
}

	
public void test5() throws Exception {
	
	//Test5 - Send Null Order object
	String expected = "Object is Null";
	
	o1 = null;
	try{
	
		server.getServer().calculatePayment(o1);	
	}
	catch(InvalidException e)
	{
		Assert.assertEquals(expected,e.getMessage());	
	}
	
}

		
		
}






